<?php
namespace app\wechat\controller;


use think\View;
use think\Loader;
use think\Image;

class RWallet extends Common
{
    // 用户钱包首页
    public function index()
    {
        $id = session('member_id');
        // 用户余额
        $repair = db('repair')->field('id,money, money_frozen')->where('member_id', $id)->find();
        // 待结算金额
        $order = db('order_server')->alias('o')->field('sum(mm.money) as money_sum,sum(oa.return_money) as return_money_sum')
            ->join('mob_member_money mm', 'o.order_sn = mm.order_sn and mm.type=1','LEFT')
            ->join('mob_order_austin oa', 'o.id = oa.order_id and oa.audit_type=1 and (oa.status=1 or oa.result=2) ','LEFT')
            ->join('mob_order_product_server ops', 'oa.product_id = ops.id ','LEFT')
            ->where('o.repair_id', $repair['id'])
            ->where('o.status', 3)->find();

        // 获取用户银行卡列表
        $bank_num = db('member_fund_account')->where('member_id', $id)->count();

        $this->assign('data',[
            'money' => $repair["money"],
            'money_frozen' => $repair["money_frozen"],
            'money_f' => number_format( $order["money_sum"]-$order["return_money_sum"] , 2, '.', ''),
            'bank_num' => $bank_num
        ]);
        return view();
    }

    // 交易明细页面
    public function detail() {
        if (request()->isAjax()) {
            $param['member_id'] = session('member_id');
            $param['type'] = input('param.type');
            $data = call_interface('member_money_bill',$param);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('w_wallet/detail_tpl');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view();
    }
    // 收支明细页面
    public function detail_sz() {
        if (request()->isAjax()) {
            $param['member_id'] = session('member_id');
            $param['type'] = input('param.type');
            $data = call_interface('member_money_bill_sz',$param);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('r_wallet/detail_tpl_sz');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view();
    }

    // 待结算金额明细页面
    public function detail_f() {
        if (request()->isAjax()) {
            $param['member_id'] = session('member_id');
            $param['type'] = input('param.type');
            $data = call_interface('member_money_bill_f',$param);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('r_wallet/detail_tpl_f');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view();
    }

    // 今日收入明细页面
    public function detail_today() {
        if (request()->isAjax()) {
            $param['member_id'] = session('member_id');
            $param['type'] = input('param.type');
            $data = call_interface('member_money_bill_today',$param);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('r_wallet/detail_tpl_today');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view();
    }

    // 账单明细详情
    public function detail_info() {
        $param = [
            'detail_id' => input('param.id'),
            'member_id' => session('member_id')
        ];
        $data = call_interface('get_detail_info',$param);
        if ($data['code'] == 200) {
            $this->assign('data', $data['data']);
            return view();
        } else {
            return view('public/error');
        }
    }

    // 银行卡列表页面
    public function banks() {
        $param['member_id'] = session('member_id');
        $type = input('param.type');
        $banks = call_interface('get_member_fund_account',$param);
        $this->assign('banks', $banks['data']);
        $this->assign('type', $type);
        return view();
    }

    // 添加银行卡列表页面
    public function save_bank() {
        $fund_account_id = input('param.fund_account_id', 0);
        if (request()->isPost()) {
            $type = input('param.account_type'); // 银行卡类型
            switch ($type) {
                case 1: //  添加支付宝
                    break;
                case 2: // 添加微信
                    break;
                case 3:
                    $param = [
                        'member_id' => session('member_id'),
                        'type' => $type,
                        'account' => input('param.account'),
                        'phone' => input('param.phone'),
                        'householder' => input('param.householder'),
                        'id_card' => input('param.id_card'),
                        'fund_account_id' => input('param.fund_account_id')
                    ];
                    $data = call_interface('save_member_fund_account',$param);
                    break;
                default:
                    break;
            }
            return json($data);
        } else {
            $member_fund_account = [];
            if ($fund_account_id > 0) {
                $member_fund_account = db('member_fund_account')->where('id', $fund_account_id)->find();
            }
            $this->assign('member_fund_account', $member_fund_account);

            // 检查是否实名认证
            $type = input('param.type'); // 用户角色类型
            $models = ['member', 'repair', 'dealer', 'partner'];
            $model = $models[$type];
            if ($type == 0) {
                $where['id'] = session('member_id');
            } else {
                $where['member_id'] = session('member_id');
            }
            $certification = db($model)->field('is_certification, proposer, card_no')->where($where)->find();
            $this->assign('certification', $certification);
            $this->assign('type', $type);

            return view();
        }
    }

    // 银行卡解除绑定
    public function relieve() {
        $param = [
            'member_id' => session('member_id'),
            'fund_account_id' => input('param.bank_id')
        ];
        $data = call_interface('relieve_bank_band',$param);
        return json($data);
    }

    // 充值
    public function recharge() {
        $type = input('param.type', 0);
        $this->assign('type', $type);
        return view();
    }

    // 提现
    public function withdraw() {
        $type = input('param.type');
        if( request()->isAjax()) {
            $param = [
                'member_id' => session('member_id'),
                'type' => $type,
                'fund_account' => input('param.account'),
                'fund_account_id' => input('param.fund_account_id'),
                'fund_account_type' => input('param.fund_account_type'),
                'code' => input('param.code'),
                'money' => input('param.money'),
                'withdraw_poundage' => input('param.withdraw_poundage')
            ];
            $data = call_interface('member_withdraw',$param);
            return json($data);
        } else {
            $param = [
                'member_id' => session('member_id'),
                'type' => $type
            ];
            $data = call_interface('withdraw_index',$param);
            //dump($data);
            if ($data['code'] != 200) {
                return view('public/error');
            }
            $this->assign('data', $data['data']);
            $this->assign('type', $type);
            return view();
        }

    }

    // 我要收款功能
    public function repair_gathering() {
        $repair_id = input('param.repair_id');
        $repair = db('repair')->alias('r')->field('r.id, r.repair_name, r.logo, m.phone')
            ->join('mob_member m', 'r.member_id = m.id')
            ->where('r.id', $repair_id)->find();

        $this->assign('repair', $repair);
        return view();
    }

    // 我要收款--二维码
    public function gathering() {
        $member_id = session('member_id');
        $repair = db('repair')->field('gathering_qr_code, id, repair_name, logo')->where('member_id', $member_id)->find();
        $erweima_text = 'http://' . $_SERVER['HTTP_HOST'] . url('repair_gathering', ['repair_id' => $repair['id'], 'action' => 'gathering_']);
        //if (!$repair['gathering_qr_code'] || !is_file('./'$repair['gathering_qr_code'])) {
            // 二维码不存在  生成二维码
        Loader::import('QRcode.phpqrcode');
        $erweima_image = 'gathering' . DS . 'repair' . $repair['id'] . '.png';  // 收款二维码地址
        //$erweima_url = ROOT_PATH . 'public' . DS . 'uploads' . DS .$erweima_image;
        $erweima_url = config('new.upload_img_save_dir') . DS .$erweima_image;
        if (is_file($erweima_url)) {
            unlink($erweima_url);
        }

        if (!is_dir(config('new.upload_img_save_dir') . DS . 'gathering')) {
            mkdir(config('new.upload_img_save_dir') . DS . 'gathering', 0777, true);
        }

        \QRcode::png($erweima_text, $erweima_url, '', 6, 2); //生成二维码

        $image = Image::open('./' . $repair['logo']);
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
        $logo = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'repair' . DS .  'logo'.$repair['id'].'.png';
        $image->thumb(38, 38, Image::THUMB_CENTER)->save( $logo);

        erweima_add_log_($erweima_url, $logo); //添加logo
        //db('repair')->where('id', $repair['id'])->update(['gathering_qr_code' => $erweima_image]);
        $repair['gathering_qr_code'] = $erweima_image;
        //}
        $this->assign('repair', $repair);
        return view();
    }

    // 生成收款图片
    public function create_gathering_img() {
        try {
            $id = input('param.id');
            $repair_name = input('param.repair_name');
            $dst_path = ROOT_PATH . 'public' . DS . 'static' . DS . 'images' . DS . 'save_gathering_bg.jpg';  // 背景图
            $src_path = ROOT_PATH . 'public' . DS . 'uploads' . DS . input('param.img');  // 二维码

            //创建图片的实例
            $dst = imagecreatefromstring(file_get_contents($dst_path));
            $src = imagecreatefromstring(file_get_contents($src_path));

            //获取背景图片的宽高
            list($dst_w, $dst_h) = getimagesize($dst_path);
            //获取二维码的宽高
            list($src_w, $src_h) = getimagesize($src_path);
            $w = ($dst_w - $src_w) / 2;

            //将水印图片复制到目标图片上，最后个参数50是设置透明度，这里实现半透明效果
            //imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 50);
            //如果水印图片本身带透明色，则使用imagecopy方法
            imagecopy($dst, $src, $w, 168, 0, 0, $src_w, $src_h);

            $font = ROOT_PATH . 'public' . DS . 'static' . DS . 'simsun.ttc';//字体
            $box = imagettfbbox(13, 0, $font, $repair_name);
            $text_width = $box[4] - $box[6];
            $tw = ($dst_w - $text_width) / 2;

            $black = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
            imagefttext($dst, 13, 0, $tw, 433, $black, $font, $repair_name);

            //输出图片
            $image = DS . 'uploads' . DS .'gathering' . DS . 'save_repair' . $id . '.jpg';
            imagepng($dst, ROOT_PATH . 'public' . $image);

            //$gathering_code_save = db('repair')->where('id', $id)->value('gathering_code_save');

            //db('repair')->where('id', $id)->update(['gathering_code_save' => $image]);

            //unlink('.' . $gathering_code_save); // 生成成功  删除原图

            return json(['code' => 200, 'path' => $image]);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    // 显示门店图片
    public function gathering_img() {
        return view();
    }
}
